package com.launch.wlfw.controller;

import com.github.pagehelper.PageInfo;
import com.launch.wlfw.common.Result;
import com.launch.wlfw.pojo.Admin;
import com.launch.wlfw.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author liuwei
 * @ClassName AdminController.java
 * @description: TODO
 * @date 2023年07月01日
 * @version: 1.0
 */
@Slf4j
@Controller
public class AdminController {
    @Resource
    AdminService adminService;


    /**
     * 查询管理员列表
     */
    @GetMapping("/adminAll")
    @ResponseBody
    public Result<PageInfo<Admin>> page(Admin admin, @RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "15") Integer pageSize){
        return Result.success(adminService.findByName(admin, pageNum, pageSize));
    }

    @RequestMapping("/adminIndex")
    public String adminIndex() {
        return "admin/adminIndex";
    }


    /**
     * 添加管理员
     * @return {@link String}
     */
    @RequestMapping("/addAdminSubmit")
    @ResponseBody
    public Result<Admin> add(@RequestBody Admin admin){
        adminService.addAdminList(admin);
        return Result.success(admin);
    }

    @RequestMapping("/adminAdd")
    public String selectAddAdmin(){
        return "admin/adminAdd";
    }

    /**
     * 删除管理员信息
     *
     */
    @RequestMapping("/deleteAdminByIds")
    @ResponseBody
    public Result<Admin>  delete(String ids){
        adminService.deleteAdminList(ids);
        return Result.success();
    }


    @RequestMapping("/queryAdminById")
    public String queryAdminById(Integer id, Model model){
        model.addAttribute("id", id);
        return "admin/updateAdmin";
    }


    /**
     * 修改密码
     */
    @RequestMapping("/updatePwdSubmit")
    @ResponseBody
    public Result<Admin> loginChangePassword(Integer id,String oldPwd,String newPwd) {
        log.debug("id+oldPwd+newPwd======"+ id + "    "+oldPwd + "      "+newPwd);
        adminService.updateAdminPassword(id,oldPwd,newPwd);
        return Result.success();
    }




}
